<?php
namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\Main;

class XhschoolAppgroupapp extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iAGAId;

    /**
     *
     * @var integer
     */
    public $iSAGId;

    /**
     *
     * @var integer
     */
    public $iSASId;

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     */
    public $bAGAEnable;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHSchool_AppGroupApp");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_AppGroupApp';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolAppgroupapp[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolAppgroupapp
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据应用组ID，类型，学校ID，组成查询组内应用及组外应用语句并返回
     *
     * @param $groupId      --应用组ID
     * @param $type         --类型（in或out）
     * @param $schoolId     --学校ID
     * @return mixed
     *
     * create by ck 20160930
     * modify by ck 20161008    sql语句修改，修复问题
     */
    public static function createSqlForAppInfoByGroupId($groupId, $type, $schoolId)
    {
        $queryArr['columns'] = ['B.iSASId', 'A.iAppId', 'A.iAppCode', 'A.sAppName', 'A.iAppType', 'A.sAppPackage', 'A.iAppSystem'];
        if($type == 'in'){
            $queryArr['query'] = "SELECT :columns: FROM XHSchool_AppGroupApp AS B LEFT JOIN XHSchool_AppStore AS S ON B.iSASId = S.iSASId AND B.iSAGId = {$groupId} AND B.bDelete = 0 AND S.bDelete = 0 AND S.iSchoolId = {$schoolId} LEFT JOIN XHApp_Store AS A ON S.iAppId = A.iAppId WHERE A.bDelete = 0";
        }
        else{
//            $queryArr['query']   = "SELECT :columns: FROM (SELECT S.* FROM XHSchool_AppStore AS S LEFT JOIN XHSchool_AppGroupApp AS G ON S.iSASId = G.iSASId AND G.iSAGId = {$groupId} WHERE S.bDelete = 0 AND G.iAGAId IS NULL AND S.iSchoolId = {$schoolId}) AS B LEFT JOIN XHApp_Store AS A ON B.iAppId = A.iAppId AND A.bDelete = 0";
            $queryArr['query']   = "SELECT :columns: FROM (SELECT S.* FROM XHSchool_AppStore AS S LEFT JOIN (SELECT * FROM XHSchool_AppGroupApp WHERE iSAGId = {$groupId} AND bDelete = 0) AS G ON S.iSASId = G.iSASId WHERE G.iAGAId IS NULL AND S.iSchoolId = {$schoolId}) AS B LEFT JOIN XHApp_Store AS A ON B.iAppId = A.iAppId AND A.bDelete = 0";
        }
        return $queryArr;
    }

    /**
     * 根据学校应用ID，应用组ID，学校ID关联应用
     *
     * @param $schAppIdArr
     * @param $groupId
     * @param $schoolId
     * @return mixed
     *
     * create by ck 20161008
     */
    public static function contactSchoolGroupApp($schAppIdArr, $groupId, $schoolId)
    {
        $userId = Main::getLoginUserInfo('userId');
        $sql = 'INSERT INTO XHSchool_AppGroupApp (iSAGId,iSASId,iSchoolId,bAGAEnable,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $valueArr = array();
        $nowTime = date('Y-m-d H:i:s');
        foreach($schAppIdArr as $schAppId){
            $valueArr[] = "({$groupId},{$schAppId},{$schoolId},1,0,'{$nowTime}',{$userId},'{$nowTime}',{$userId})";
        }
        $sql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($sql);
    }

    /**
     * 根据学校应用ID，应用组ID，学校ID取消关联应用
     *
     * @param $schAppIdArr
     * @param $groupId
     * @param $schoolId
     * @return mixed
     *
     * create by ck 20161008
     */
    public static function deleteSchoolGroupApp($schAppIdArr, $groupId, $schoolId)
    {
        return self::find([
            'conditions' => 'bDelete = 0 AND iSAGId = :iSAGId: AND iSchoolId = :iSchoolId: AND iSASId IN ({idArr:array})',
            'bind'       => ['iSAGId'=>$groupId, 'iSchoolId'=>$schoolId, 'idArr'=>$schAppIdArr]
        ])->delete();
    }

}
